package com.wxproject.demo.Service.impl;

import com.wxproject.demo.Entity.AO.ChatMessageAO;
import com.wxproject.demo.Entity.AO.ChatUserAO;
import com.wxproject.demo.Entity.AO.SenderAO;
import com.wxproject.demo.Entity.VO.ChatMessageVO;
import com.wxproject.demo.Entity.VO.ChatRoomRecord;
import com.wxproject.demo.Entity.VO.RoomVO;
import com.wxproject.demo.Mapper.ChatMapper;
import com.wxproject.demo.Service.ChatService;
import com.wxproject.demo.Utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author zqf
 * @date 2021/3/27 10:32
 * @description
 */

@Service
public class ChatServiceImpl implements ChatService {
    @Autowired
    private ChatMapper chatMapper;
    @Override
    public Integer saveMessage(SenderAO senderAO) {
        return chatMapper.saveMessage(senderAO);
    }

    @Override
    public List<ChatMessageVO> getChatMessage(ChatUserAO chatUserAO) {
        List<ChatMessageVO> chatMessage = chatMapper.getChatMessage(chatUserAO);
        for (int i = 0; i <chatMessage.size() ; i++) {
            ChatMessageVO chatMessageVO = chatMessage.get(i);
            String senderAvatarUrl= RedisUtil.hmget("userInfo-"+chatMessageVO.getSenderName()).get("avatarUrl").toString();
            chatMessage.get(i).setSenderAvatarUrl(senderAvatarUrl);
            String receiverAvatarUrl = RedisUtil.hmget("userInfo-"+chatMessageVO.getReceiverName()).get("avatarUrl").toString();
            chatMessage.get(i).setReceiverAvatarUrl(receiverAvatarUrl);
        }
        return chatMessage;
    }

    @Override
    public List<String> getAllRoom() {
        return chatMapper.getAllRoom();
    }

    @Override
    public String getRoomIdByTypeId(String typeId) {
        return chatMapper.getRoomIdByTypeId(typeId);
    }

    @Override
    public List<ChatRoomRecord> getChatRoomRecord(String roomId) {
        List<ChatRoomRecord> chatRoomRecord = chatMapper.getChatRoomRecord(roomId);
        for (int i = 0; i <chatRoomRecord.size() ; i++) {
            ChatRoomRecord chatRoomRecord1 = chatRoomRecord.get(i);
            String avatarUrl= RedisUtil.hmget("userInfo-"+chatRoomRecord1.getSenderName()).get("avatarUrl").toString();
            chatRoomRecord.get(i).setAvatarUrl(avatarUrl);
        }
        System.out.println(chatRoomRecord.toString());
        return chatRoomRecord;
    }

    @Override
    public Integer saveChatRoomRecord(ChatMessageAO chatMessageAO) {
        return chatMapper.saveChatRoomRecord(chatMessageAO);
    }

    @Override
    public Integer getRoomName(String roomName) {
        return chatMapper.getRoomName(roomName);
    }

    @Override
    public String getRoomNameByTypeId(String typeId) {
        return chatMapper.getRoomNameByTypeId(typeId);
    }

    @Override
    public List<RoomVO> getSocketRoom() {
        return chatMapper.getSocketRoom();
    }

    @Override
    public String getRoomNameByRoomId(String roomId) {
        return chatMapper.getRoomNameByRoomId(roomId);
    }

}
